<?php

namespace App\Http\Controllers\Admin;

use App\Services\WxCardService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * 微信卡券-领取记录控制器
 * @package App\Http\Controllers\Admin
 */
class WxCardReceiveController extends BaseController
{
    protected $wxCardSer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->wxCardSer = new WxCardService();
    }

    /**
     * 领券记录列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxCardReceive::with('user')->select('*');
            $cardId = $request->get('card_id', '');
            if ($cardId) {
                $query->where('card_id',$cardId);
            }
            $userCardCode = $request->get('user_card_code','');
            if ($userCardCode) {
                $query->where('user_card_code', $userCardCode);
            }
            $openid = $request->get('openid', '');
            if ($openid){
                $query->where('user_openid', $openid);
            }
            $outerStr = $request->get('outer_str', '');
            if ($outerStr){
                $query->where('outer_str', $outerStr);
            }
            return $query->orderBy('id', 'DESC')->paginate($pageSize);
        }
        return view('admin.wxcardreceive.index', $this->viewData);
    }

    /**
     * 更改卡号
     * @param Request $request
     */
    public function updateCode(Request $request)
    {
        try {
            $receiveId = $request->input('receive_id', 0);
            $newCode = $request->input('code', 0);
            $rs = $this->wxCardSer->updateCode($receiveId, $newCode);
            if ($rs) {
                return $this->showMsg('success', '更改用户卡号成功', [], 'admin.wxcardreceive.index');
            }
            throw new \Exception('更改用户卡号失败');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 设置无效
     * @param Request $request
     */
    public function disable(Request $request)
    {
        try {
            $receiveId = $request->input('receive_id', 0);
            $rs = $this->wxCardSer->disable($receiveId);
            if ($rs) {
                return $this->showMsg('success', '设置失效成功', [], 'admin.wxcardreceive.index');
            }
            throw new \Exception('设置失效失败');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 转赠列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function transfer(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxCardTransfer::with('user')->select('*');
            $cardId = $request->get('card_id', '');
            if ($cardId) {
                $query->where('card_id',$cardId);
            }
            $userCardCode = $request->get('user_card_code','');
            if ($userCardCode) {
                $query->where('user_card_code', $userCardCode);
            }
            $openid = $request->get('openid', '');
            if ($openid){
                $query->where('user_openid', $openid);
            }
            return $query->orderBy('id', 'DESC')->paginate($pageSize);
        }
        return view('admin.wxcardreceive.transfer', $this->viewData);
    }
}
